$(function () {
    userCheck();
    codeClick();


    function userCheck() {
        var user = $('#user');
        var psw=$('#psw');
        var submit=$('#submit');
        var code=$('#code');
        var isBool=false;
        user.blur(function () {
            var _this = $(this);
            var datas = $(this).val();
            if(datas){
                var Obj = {name: datas, type: 'getName'};
                requestEvent('post', '../admin/login.php', Obj, function (res) {
                    if (!res.status) {
                        _this.parents('li').find('span').text('*用户' + datas + '未注册');
                        isBool=false;
                    } else {
                        _this.parents('li').find('span').text('');
                        isBool=true;
                    }
                });
            }
            if(isBool){
                psw.prop("disabled",false);
                code.prop("disabled",false);
            }
        });
        submit.tap(function(){
            if(isBool){
                var userText=user.val();
                var pswText=psw.val();
                var codeText=code.val();
                var Obj = {name: userText, psw: pswText,code:codeText};
                requestEvent('post', '../admin/login.php', Obj, function (res) {
                    if (res.status==2) {
                        psw.parents('li').find('span').text(res.msg);
                    } else if(res.status==3) {
                        code.parents('li').find('span').text(res.msg);
                    }else if(res.status==1){
                        psw.parents('li').find('span').text('');
                        code.parents('li').find('span').text('');
                        window.location.href='home.html';
                    }
                });
            }
        })
    }

    /*验证码点击*/
    function codeClick(){
        var validate=$('#validate');
        validate.tap(function(){
            $(this).attr('src', '../admin/code.php?w=98&h=48&fs=24&randon=' + Math.random());
        })
    }
});
